package view;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.swing.JFrame;

import model.Clock;
import model.command.LEDOffCommand;
import controller.EventController;
import javax.swing.SpringLayout;
import javax.swing.JSlider;
import javax.swing.SwingConstants;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JButton;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.UIManager;
import javax.swing.JTextField;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

/**
 * @(#) GuiImpl.java
 */

@SuppressWarnings("serial")
public class GuiImpl extends JFrame implements Gui
{
	static final int FPS_MIN = 0;
	static final int FPS_MAX = 240;
	static final int FPS_INIT = 120;
	private final static int FLASH_DURATION = 50;
	private JLabel labelTempoValue;
	private JLabel labelMeasureValue;
	
	public  void run(){ //GuiImpl() {
		setResizable(false);
		setTitle("Metronome");
		SpringLayout springLayout = new SpringLayout();
		getContentPane().setLayout(springLayout);
		
		JSlider slider = new JSlider(JSlider.HORIZONTAL,FPS_MIN, FPS_MAX, FPS_INIT);
		springLayout.putConstraint(SpringLayout.EAST, slider, -273, SpringLayout.EAST, getContentPane());
		slider.setOrientation(SwingConstants.VERTICAL);
		slider.setMajorTickSpacing(60);
		slider.setMinorTickSpacing(10);
		slider.setPaintTicks(true);
		slider.setPaintLabels(true);
		slider.addChangeListener(new ChangeListener() {
			
			@Override
			public void stateChanged(ChangeEvent e) {
				// TODO Auto-generated method stub
				controller.ChangewheelValue( ((JSlider)e.getSource()).getValue());
			}
		});
		getContentPane().add(slider);
		
		JLabel lblTempo = new JLabel("Tempo");
		springLayout.putConstraint(SpringLayout.NORTH, slider, 11, SpringLayout.NORTH, lblTempo);
		springLayout.putConstraint(SpringLayout.WEST, lblTempo, 30, SpringLayout.WEST, getContentPane());
		springLayout.putConstraint(SpringLayout.EAST, lblTempo, 79, SpringLayout.WEST, getContentPane());
		lblTempo.setFont(new Font("Tahoma", Font.BOLD, 11));
		getContentPane().add(lblTempo);
		
		JButton btnStop = new JButton("Stop");
		btnStop.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				controller.stop();
			}
		});
		springLayout.putConstraint(SpringLayout.SOUTH, btnStop, -10, SpringLayout.SOUTH, getContentPane());
		getContentPane().add(btnStop);
		
		JButton btnStart = new JButton("start");
		btnStart.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				controller.start();
			}
		});
		springLayout.putConstraint(SpringLayout.NORTH, lblTempo, 4, SpringLayout.NORTH, btnStart);
		springLayout.putConstraint(SpringLayout.NORTH, btnStart, 6, SpringLayout.NORTH, getContentPane());
		springLayout.putConstraint(SpringLayout.EAST, btnStop, 0, SpringLayout.EAST, btnStart);
		springLayout.putConstraint(SpringLayout.EAST, btnStart, -10, SpringLayout.EAST, getContentPane());
		getContentPane().add(btnStart);
		
		labelTempoValue = new JLabel(""+FPS_INIT);
		labelTempoValue.setForeground(new Color(0, 153, 153));
		labelTempoValue.setBackground(UIManager.getColor("Label.disabledShadow"));
		springLayout.putConstraint(SpringLayout.NORTH, labelTempoValue, 79, SpringLayout.SOUTH, lblTempo);
		springLayout.putConstraint(SpringLayout.SOUTH, labelTempoValue, -109, SpringLayout.SOUTH, getContentPane());
		springLayout.putConstraint(SpringLayout.WEST, slider, 6, SpringLayout.EAST, labelTempoValue);
		springLayout.putConstraint(SpringLayout.WEST, labelTempoValue, 10, SpringLayout.WEST, getContentPane());
		springLayout.putConstraint(SpringLayout.EAST, labelTempoValue, -346, SpringLayout.EAST, getContentPane());
		labelTempoValue.setHorizontalAlignment(SwingConstants.CENTER);
		labelTempoValue.setFont(new Font("Tahoma", Font.BOLD, 16));
		getContentPane().add(labelTempoValue);
		
		btnInc = new JButton("Inc");
		btnInc.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				controller.changeMeasureID(1);
			}
		});
		springLayout.putConstraint(SpringLayout.NORTH, btnInc, 65, SpringLayout.NORTH, getContentPane());
		springLayout.putConstraint(SpringLayout.WEST, btnInc, 46, SpringLayout.EAST, slider);
		getContentPane().add(btnInc);
		
		btnDec = new JButton("Dec");
		btnDec.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				controller.changeMeasureID(-1);
			}
		});
		springLayout.putConstraint(SpringLayout.WEST, btnDec, 0, SpringLayout.WEST, btnInc);
		springLayout.putConstraint(SpringLayout.SOUTH, btnDec, -69, SpringLayout.SOUTH, getContentPane());
		getContentPane().add(btnDec);
		
		labelMeasureValue = new JLabel("4");
		springLayout.putConstraint(SpringLayout.NORTH, labelMeasureValue, 24, SpringLayout.NORTH, labelTempoValue);
		springLayout.putConstraint(SpringLayout.EAST, labelMeasureValue, -117, SpringLayout.EAST, getContentPane());
		labelMeasureValue.setFont(new Font("Tahoma", Font.BOLD, 12));
		labelMeasureValue.setHorizontalAlignment(SwingConstants.CENTER);
		getContentPane().add(labelMeasureValue);
		
		
		LEDTempo = new JTextField();
		springLayout.putConstraint(SpringLayout.NORTH, LEDTempo, 55, SpringLayout.SOUTH, btnStart);
		LEDTempo.setEnabled(false);
		LEDTempo.setBackground(new Color(240, 240, 240));
		LEDTempo.setEditable(false);
		getContentPane().add(LEDTempo);
		LEDTempo.setColumns(10);
		
		LEDMeasure = new JTextField();
		springLayout.putConstraint(SpringLayout.WEST, LEDTempo, 0, SpringLayout.WEST, LEDMeasure);
		springLayout.putConstraint(SpringLayout.SOUTH, LEDTempo, -54, SpringLayout.NORTH, LEDMeasure);
		springLayout.putConstraint(SpringLayout.EAST, LEDTempo, 0, SpringLayout.EAST, LEDMeasure);
		springLayout.putConstraint(SpringLayout.EAST, LEDMeasure, -26, SpringLayout.EAST, getContentPane());
		springLayout.putConstraint(SpringLayout.SOUTH, LEDMeasure, -64, SpringLayout.NORTH, btnStop);
		LEDMeasure.setEditable(false);
		LEDMeasure.setEnabled(false);
		LEDMeasure.setBackground(new Color(240, 240, 240));
		getContentPane().add(LEDMeasure);
		LEDMeasure.setColumns(10);
		
		JLabel lblLed = new JLabel("LED 1");
		springLayout.putConstraint(SpringLayout.NORTH, lblLed, 55, SpringLayout.SOUTH, btnStart);
		getContentPane().add(lblLed);
		
		JLabel lblLed_1 = new JLabel("LED 2");
		springLayout.putConstraint(SpringLayout.WEST, lblLed, 0, SpringLayout.WEST, lblLed_1);
		springLayout.putConstraint(SpringLayout.EAST, lblLed_1, -43, SpringLayout.EAST, getContentPane());
		springLayout.putConstraint(SpringLayout.WEST, LEDMeasure, 6, SpringLayout.EAST, lblLed_1);
		springLayout.putConstraint(SpringLayout.NORTH, LEDMeasure, 0, SpringLayout.NORTH, lblLed_1);
		springLayout.putConstraint(SpringLayout.SOUTH, lblLed_1, -64, SpringLayout.NORTH, btnStop);
		getContentPane().add(lblLed_1);
		
		JLabel lblMeasure = new JLabel("Mesure    -->");
		lblMeasure.setFont(new Font("Tahoma", Font.BOLD, 11));
		springLayout.putConstraint(SpringLayout.WEST, lblMeasure, 0, SpringLayout.WEST, btnInc);
		springLayout.putConstraint(SpringLayout.SOUTH, lblMeasure, 0, SpringLayout.SOUTH, labelMeasureValue);
		getContentPane().add(lblMeasure);
		
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setLocation(600,400);
		this.setSize(450,300);
		this.setVisible(true);
		
	}
	private EventController controller;
	private Clock clock;
	private JTextField LEDTempo;
	private JTextField LEDMeasure;
	private JButton btnInc;
	private JButton btnDec;
	Clip beep;
	
	/**
	 * @param tempo
	 */
	public void printTempo( int tempo )
	{
		labelTempoValue.setText(""+tempo);
	}
	
	/**
	 * @param LEDNumber
	 */
	public void flashLED( int LEDNumber )
	{
		if (LEDNumber == 1){
			beep(1);
			LEDTempo.setBackground(new Color(0, 255, 153));
			LEDOffCommand cmd = new LEDOffCommand();
			cmd.setGui(this);
			cmd.setLEDNumber(1);
			clock.activateAfterdelay(cmd, FLASH_DURATION);
		}
		else{
			beep(2);
			LEDMeasure.setBackground(new Color(255, 0, 0));
			LEDOffCommand cmd = new LEDOffCommand();
			cmd.setLEDNumber(2);
			cmd.setGui(this);
			clock.activateAfterdelay(cmd, FLASH_DURATION);
		}
		
	}

	public JTextField getLEDTempo() {
		return LEDTempo;
	}

	public JTextField getLEDMeasure() {
		return LEDMeasure;
	}

	public EventController getController() {
		return controller;
	}

	public void setController(EventController controller) {
		this.controller = controller;
	}

	/**
	 * @return the clock
	 * retourne la valeur clock
	 */
	public Clock getClock() {
		return clock;
	}

	/**
	 * @param clock the clock to set
	 */
	public void setClock(Clock clock) {
		this.clock = clock;
	}

	@Override
	public void putLEDOff(int LEDNumber) {
		
		if (LEDNumber == 1){
			LEDTempo.setBackground(new Color(240, 240, 240));
		}
		else{
			LEDMeasure.setBackground(new Color(240, 240, 240));
		}
	}

	@Override
	public void PrintMeasure(int measure) 
	{
		if(measure == 2)
			btnDec.setEnabled(false);
		else
			btnDec.setEnabled(true);
		
		if(measure == 7)
			btnInc.setEnabled(false);
		else
			btnInc.setEnabled(true);
		
		labelMeasureValue.setText(""+measure);
	}
	
	public JLabel getLabelTempoValue() {
		return labelTempoValue;
	}

	public JLabel getLabelMeasureValue() {
		return labelMeasureValue;
	}

	public JButton getBtnInc() {
		return btnInc;
	}

	public JButton getBtnDec() {
		return btnDec;
	}

	public void beep(int type){
		try {
			beep = AudioSystem.getClip();
			AudioInputStream ais;
			if (type == 1)
				ais = AudioSystem.getAudioInputStream(new File("src/song/beep.wav"));
			else
				ais = AudioSystem.getAudioInputStream(new File("src/song/beep2.wav"));
			beep.open(ais);
			beep.start();
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
}
